package com.ctgu.springstart.controller;

import com.ctgu.springstart.domain.JsonData;
import com.ctgu.springstart.domain.ServerSettings;
import com.ctgu.springstart.domain.User;
import com.ctgu.springstart.task.AsyncTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;


@RestController
public class GetController {

    private Map<String, Object> params = new HashMap<>();

    @Autowired
    private ServerSettings serverSettings;

    @Autowired
    private AsyncTask asyncTask;

//    @RequestMapping(path = "/{city_id}/{user_id}",method = RequestMethod.GET)
//    public Object findUser(@PathVariable("city_id") String cityId,
//                           @PathVariable("user_id") String userId){
//        params.clear();
//        params.put("cityId", cityId);
//        params.put("userId", userId);
//        return params;
//    }

//    @GetMapping(path = "/{city_id}/{user_id}")
//    public Object findUser(@PathVariable("city_id") String cityId,
//                           @PathVariable("user_id") String userId){
//        params.clear();
//        params.put("cityId", cityId);
//        params.put("userId", userId);
//        return params;
//    }

    @GetMapping(value="/v1/page_user1")
    public Object pageUser(int from, int size){
        params.clear();
        params.put("from", from);
        params.put("size", size);
        return params;
    }

    @GetMapping(value="/v1/testProperties")
    public ServerSettings testProperties(){
        return serverSettings;
    }

    @GetMapping(value="/api/v1/testProperties")
    public ServerSettings testProperties1(){
        return serverSettings;
    }

    @GetMapping(value="/v1/page_user2")
    public Object pageUserV2(@RequestParam(defaultValue = "0", name = "page", required = true) int from, int size){
        params.clear();
        params.put("from", from);
        params.put("size", size);
        return params;
    }

    @RequestMapping("/v1/save_user")
    public Object saveUser(@RequestBody User user){
        params.clear();
        params.put("user", user);
        return params;
    }

    @GetMapping("/v1/get_header")
    public Object getHeader(@RequestHeader("access_token") String accessToken, String id){
        params.clear();
        params.put("access_token", accessToken);
        params.put("id", id);
        return params;
    }

    @GetMapping("/v1/test_request")
    public Object testRequest(HttpServletRequest request){
        params.clear();
        params.put("id", request.getParameter("id"));
        return params;
    }

    @GetMapping("/api2/v1/account")
    public Object account(){
        params.clear();
        System.out.println("controller----->account");
        params.put("money", 1000);
        return params;
    }

    @GetMapping("/api/v1/async_task")
    public JsonData exeAsyncTask() throws InterruptedException{
        long begin = System.currentTimeMillis();
//        asyncTask.task1();
//        asyncTask.task2();
//        asyncTask.task3();
        Future<String> task4 = asyncTask.task4();
        Future<String> task5 = asyncTask.task5();
        Future<String> task6 = asyncTask.task6();
        for(;;){
            if(task4.isDone()&&task5.isDone()&&task6.isDone()){
                break;
            }
        }
        long end = System.currentTimeMillis();
        long time = end-begin;
        System.out.println("执行总耗时:" + time);
        return JsonData.builder().data(time).build();
    }

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/api/v1/log")
    public JsonData testLog(){
        logger.debug("this is debug");
        logger.info("this is info level");
        logger.warn("this is warn");
        logger.error("this is error");
        return JsonData.builder().msg("log record").build();
    }

}
